package com.seven.gitautopush;

import cn.hutool.core.io.FileUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.CronTask;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.seven.gitautopush.config.GitConfig;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.transport.ChainingCredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

public class GitAutoPushApplication {

    static CopyOnWriteArrayList<File> result = new CopyOnWriteArrayList<>();


    public static void main(String[] args) throws IOException {
        for (String arg : args) {
            System.out.println(arg);
            //配置
            if (arg.startsWith("--application=")) {
                String[] split = arg.split("=");
                GitConfig.initConfig(split[1]);
            }
        }
        System.out.println(GitConfig.getGitConfig());
    }

    public static void t() {
        /*读取配置*/
        String gitJsonFile = "G:\\项目\\java\\git-auto-push\\src\\main\\resources\\git.json";
        String gitJsonDb = FileUtil.readString(gitJsonFile, StandardCharsets.UTF_8);
        List<String> gitFilePath = JSONUtil.parseArray(gitJsonDb).toList(String.class);
        System.out.println(gitFilePath);
        /*创建job*/
        List<Thread> threads = new ArrayList<>();
        for (File file : File.listRoots()) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    List<File> files = loopDirs(file, new FileFilter() {
                        @Override
                        public boolean accept(File pathname) {
                            if (pathname.isDirectory()) {
                                return ".git".equals(pathname.getName());
                            }
                            return false;
                        }
                    }, Arrays.asList(".idea", "src", ".git", ".mvn", ".github", "docker", "build", "target", "node_modules", "node"));
                    result.addAll(files);
                }
            });
            threads.add(thread);
        }
        for (Thread thread : threads) {
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("寻找完毕");
        System.out.println(result);
        //存储起来
        Set<String> collect = result.stream().map(File::getAbsolutePath).collect(Collectors.toSet());
        FileUtil.writeString(JSONUtil.toJsonStr(collect), gitJsonFile, StandardCharsets.UTF_8);
        /*执行push git命令*/
        for (File file : result) {
            try {
                Git open = Git.open(file);
                open.add().addFilepattern(".").call();
                open.commit().setMessage("auto commit").setAllowEmpty(false).call();
                open.push().setCredentialsProvider(new ChainingCredentialsProvider(
                        new UsernamePasswordCredentialsProvider("1399435579@qq.com", "yjl19981007"))).call();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoFilepatternException e) {
                e.printStackTrace();
            } catch (GitAPIException e) {
                e.printStackTrace();
            }
        }
    }

    public static List<File> loopDirs(File file, FileFilter fileFilter, List<String> ignoreDirName) {
        File[] files = file.listFiles();
        ArrayList<File> result = new ArrayList<>();
        if (files != null) {
            for (File f : files) {
                if (fileFilter.accept(f)) {
                    System.out.println(f);
                    result.add(f);
                }
                if (!ignoreDirName.contains(f.getName())) {
                    result.addAll(loopDirs(f, fileFilter, ignoreDirName));
                }
            }
        }
        return result;
    }
}
